
import lrz from 'lrz';

const compression = (imageFile, callback, maxSize = 350 * 1024) => {
  function compressImage(file, quality) {
    return lrz(file, { quality })
      .then(rst => {
        // 处理成功会执行
        if (rst.file.size > maxSize) {
          return compressImage(file, +(quality * 0.7).toFixed(4));
        }
        return rst;
      })
      .catch(err => {
        // 处理失败会执行
      })
      .always(() => {
        // 不管是成功失败，都会执行
      });
  }

  compressImage(imageFile, 0.3).then(rst => callback && callback(new File([rst.file], rst.origin.name, { type: rst.file.type })));
};
export default compression;
